package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.exception.ApiException;
import com.ytjj.qmyx.admin.dao.ProductDao;
import com.ytjj.qmyx.admin.dao.ProductForumDao;
import com.ytjj.qmyx.admin.model.dto.BrandProductDto;
import com.ytjj.qmyx.admin.model.request.ProductLifeRequest;
import com.ytjj.qmyx.admin.model.response.ProductForumInfoResponse;
import com.ytjj.qmyx.admin.model.response.ProductLifeResponse;
import com.ytjj.qmyx.admin.service.ProductForumService;
import com.ytjj.qmyx.mall.mapper.ProductForumInfoMapper;
import com.ytjj.qmyx.mall.model.ProductForumInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 
 *
 * @Version: 1.0
 */
@Service
public class ProductForumServiceImpl implements ProductForumService {

    @Resource
    private ProductForumDao productForumDao;
    @Resource
    private ProductDao productDao;
    @Resource
    private ProductForumInfoMapper productForumInfoMapper;

    @Override
    public CommonPage<ProductLifeResponse> getProductLife(ProductLifeRequest productLifeRequest) {
        PageHelper.startPage(productLifeRequest.getPageNum(), productLifeRequest.getPageSize());
        List<ProductLifeResponse> pageList = productForumDao.getProductLife(productLifeRequest);
        return CommonPage.restPage(pageList);
    }

    @Override
    public Integer removeForum(Integer id) {
        return productForumDao.removeForum(id);
    }

    @Override
    public Integer updateSort(Integer id, Integer sort) {
        return productForumDao.updateSort(id, sort);
    }

    @Override
    public List<ProductForumInfoResponse> getBrandShopProductList(Integer id) {
        // 查询所有商品
        List<Integer> productForumInfoList = productForumDao.getProductForumInfoById(id);
        if(productForumInfoList.size() > 0) {
            return productForumDao.getBrandShopProductList(productForumInfoList);
        }
        return Collections.emptyList();
    }

    @Override
    public List<BrandProductDto> getBrandProductsByBankerId(Integer bankerId, String productName, Integer id) {
        return productDao.getBrandProductsByBankerId(bankerId, productName, id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addBrandProduct(Integer forumId, Integer productId) {
        List<Integer> integerList = productForumDao.selectProduct(forumId);
        if(integerList.size() > 0) {
            integerList.stream().forEach(item -> {
                if(item.equals(productId)) {
                    throw new ApiException("该商品已经添加，请勿重复添加");
                }
            });
            if(integerList.size() >= 3) {
                throw new ApiException("已添加3条商品，请删除后重试！");
            }
        }
        if(productId != null) {
            ProductForumInfo productForumInfo = new ProductForumInfo();
            productForumInfo.setForumId(forumId);
            productForumInfo.setProductId(productId);
            productForumInfo.setStatus(1);
            productForumInfoMapper.insertSelective(productForumInfo);
        }
    }

    @Override
    public void removeBankerProduct(Integer id) {
        ProductForumInfo productForumInfo = new ProductForumInfo();
        productForumInfo.setId(id);
        productForumInfo.setStatus(0);
        productForumInfoMapper.updateByPrimaryKeySelective(productForumInfo);
    }

    @Override
    public void updateBankerProductSort(Integer id, Integer sort) {
        ProductForumInfo productForumInfo = new ProductForumInfo();
        productForumInfo.setId(id);
        productForumInfo.setSort(sort);
        productForumInfoMapper.updateByPrimaryKeySelective(productForumInfo);
    }
}
